package com.ruoyi.web.controller.kycinfo;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.kycinfo.domain.KycContact;
import com.ruoyi.kycinfo.domain.KycUserInfo;
import com.ruoyi.kycinfo.service.IKycService;
import com.ruoyi.kycinfo.service.IKycUserInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kycinfo.domain.KycPotential;
import com.ruoyi.kycinfo.service.IKycPotentialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-10-10
 */

@RestController
@RequestMapping("/kycinfo/potential")
public class KycPotentialController extends BaseController {
    @Autowired
    private IKycPotentialService kycPotentialService;

    @Autowired
    private IKycService kycService;

    @Autowired
    private IKycUserInfoService kycUserInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:potential:list')")
    @GetMapping("/list")
    public TableDataInfo list(KycPotential kycPotential) {
        List<KycPotential> list = new ArrayList<>();
        List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();

        if (userInfoIdList.size() == 0) {
            return getDataTable(list);
        }
        kycPotential.setUserInfoIdList(userInfoIdList);
        startPage();
        list = kycPotentialService.selectKycPotentialList(kycPotential);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:potential:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KycPotential kycPotential) {
        List<KycPotential> list = kycPotentialService.selectKycPotentialList(kycPotential);
        ExcelUtil<KycPotential> util = new ExcelUtil<KycPotential>(KycPotential.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:potential:query')")
    @GetMapping(value = "/{potentialId}")
    public AjaxResult getInfo(@PathVariable("potentialId") Long potentialId) {
        return success(kycPotentialService.selectKycPotentialByPotentialId(potentialId));
    }


    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:potential:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KycPotential kycPotential) {
        // 客户号校验，输入的客户号必须是自己的客户
        Long userInfoId = kycPotential.getUserInfoId();
        List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();
        if (!userInfoIdList.contains(userInfoId)){
            return AjaxResult.error("您没有权限访问该客户信息，请检查客户号");
        }

        KycUserInfo kycUserInfo = kycUserInfoService.selectKycUserInfoByUserInfoId(userInfoId);
        if(kycUserInfo==null){
            return AjaxResult.error("您输入的客户号信息不存在，请检查客户号");
        }


        return toAjax(kycPotentialService.insertKycPotential(kycPotential));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:potential:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KycPotential kycPotential) {
        return toAjax(kycPotentialService.updateKycPotential(kycPotential));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:potential:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{potentialIds}")
    public AjaxResult remove(@PathVariable Long[] potentialIds) {
        return toAjax(kycPotentialService.deleteKycPotentialByPotentialIds(potentialIds));
    }
}
